package Servlets;

import PackDatos.GestionBD;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class srv_matricularAlumno extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Connection cn = null;
        HttpSession session = request.getSession(true);
        try {
            //Obtengo los datos
            String codigo = request.getParameter("codigo");
            int curso = Integer.parseInt(request.getParameter("curso"));
            int alumno = Integer.parseInt(request.getParameter("alumno"));
            String empleado = ""+session.getAttribute("idEmpleado");
            System.out.println("Datos que llegan: "+codigo+" "+curso+" "+alumno);
            // Proceso
            GestionBD og = new GestionBD();
            cn = og.conectar();
            String sql = "insert into matricula(mat_codigo,id_alumno,id_empleado,id_curso) values(?,?,?,?) ";
            PreparedStatement  pstm = cn.prepareStatement(sql);
            pstm.setString(1,codigo);
            pstm.setInt(2,alumno);
            pstm.setString(3,empleado);
            pstm.setInt(4,curso);
            pstm.executeUpdate();
            sql = "insert into nota(not_practica1,not_practica2,not_practica3,not_practica4,not_prompractica,not_examparcial,not_examfinal,not_promfinal,id_alumno,id_curso) values(0,0,0,0,0,0,0,0,?,?) ";
            pstm = cn.prepareStatement(sql);
            pstm.setInt(1,alumno);
            pstm.setInt(2,curso);
            pstm.executeUpdate();
            request.setAttribute("mensaje", "Alumno matriculado Satisfactoriamente!");
        } catch (Exception ex) {
            request.setAttribute("mensaje", "Alumno ya matriculado en el Curso");
        }
        RequestDispatcher rd = request.getRequestDispatcher("matricularAlumno.jsp");
        rd.forward(request, response);
    }

}
